<!-- 残疾类别关联关系 -->
<template>
  <el-card>
    <el-row>
      <el-form ref="form" :model="searchDto" inline label-suffix=":">
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="searchDto.keyword" placeholder="请输入关键字" style="width:250px" />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-s-open" type="primary" @click="clear">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <page-table
      ref="recovery"
      :page-data="pageData"
      :loading="loading"
      :do-get-page-data="getPageData"
    >
      <el-table-column type="index" label="编号" width="80" align="center" />
      <el-table-column prop="name" label="残疾类别名称" align="center" />
      <el-table-column prop="createTime" label="创建时间" align="center" />
      <el-table-column prop="status" label="操作" align="center" width="300" fixed="right">
        <template slot-scope="scope">
          <el-tooltip content="器具分类关联关系" placement="top">
            <el-button
              type="button"
              size="mini"
              icon="el-icon-receiving"
              @click="editRelation(scope.row,'appliance')"
            >
            </el-button>
          </el-tooltip>
          <el-tooltip content="服务商关联关系" placement="top">
            <el-button
              type="button"
              size="mini"
              icon="el-icon-connection"
              @click="editRelation(scope.row,'provider')"
            >
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </page-table>

    <el-dialog @close="chooseAppliance=[]" width="40%" :visible.sync="applianceStatus" title="编辑器具分类关联关系">
      <el-form style="padding:10px 20px" label-suffix=":">
        <el-form-item label="器具分类">
          <el-select v-model="chooseAppliance" multiple placeholder="请选择" style="width: 400px">
            <el-option
              v-for="item in eqList"
              :key="item.keyword"
              :label="item.label"
              :value="item.keyword"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="saveAppliance">确定</el-button>
        <el-button @click="applianceStatus=false">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="providerStatus" title="编辑服务商关联关系">
      <el-form label-suffix=":" v-if="bindProvider.length">
        <el-form-item label="已关联服务商">
          <el-tag v-for="(tag,index) in bindProvider" :key="index" closable @close="handleClose(tag)">
            {{ tag.name }}
          </el-tag>
          <el-tooltip content="选择服务商" placement="top">
            <el-button circle icon="el-icon-plus" type="info" class="ml10" @click="chooseProvider(1)" />
          </el-tooltip>
        </el-form-item>
      </el-form>
      <el-empty v-else description="您还没有关联服务商">
        <el-button type="primary" @click="chooseProvider(0)">选择服务商</el-button>
      </el-empty>
      <div slot="footer">
        <el-button type="primary" @click="saveProvider">确定</el-button>
        <el-button @click="providerStatus=false">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog width="60%" :visible.sync="chooseProviderStatus" title="选择服务商">
      <el-table
        ref="multipleTable"
        :data="unbindProvider"
        stripe
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="编号" width="80" align="center" />
        <el-table-column prop="name" label="服务商名字" align="center" />
        <el-table-column prop="createTime" label="创建时间" align="center" />
      </el-table>
      <div slot="footer">
        <el-button type="primary" @click="handleChoose">确定</el-button>
        <el-button @click="chooseProviderStatus=false">取消</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>
<script>
export default {
  name: 'ConnectionRelation',
  data() {
    return {
      searchDto: {
        parentId: 513,
        keyword: null,
        pageIndex: 1,
        pageSize: 10
      },
      pageData: {},
      loading: false,
      applianceStatus: false,
      providerStatus: false,
      chooseProviderStatus: false,
      unbindProvider: [],
      bindProvider: [],
      bindAppliance: [],
      currentId: null,
      currentPara: {},
      multipleSelection: [],
      chooseAppliance: [],
      eqList: this.getDictionary('ASSIST_EQU_CATEGOTY')
    }
  },
  methods: {
    // 获取分页数据
    getPageData(val) {
      let tem = Object.assign(this.searchDto, val);
      this.API.getSystemDictionaryPageList(tem).then(ret => {
        this.pageData = ret.data;
      });
    },

    search() {
      this.getPageData();
    },

    clear() {
      this.$refs.form.resetFields()
      this.getPageData();
    },

    // 修改服务商关联关系
    editRelation(row, key) {
      this.currentId = row.id
      this.currentPara = { disabledTypeId: row.id, disabledTypeKey: row.keyword, disabledTypeName: row.name, }
      switch (key) {
        case 'provider':
          this.API.getServiceDisabledTypeById({ disabledTypeId: this.currentId }).then(res => {
            this.bindProvider = res.data
          })
          break;
        case 'appliance':
          this.API.getDisabledTypeAssistCategoryByTypeId({ disabledTypeId: this.currentId }).then(res => {
            this.bindAppliance = res.data
            this.bindAppliance.map(v => {
              this.chooseAppliance.push(v.categoryKey)
            })
          })
          break;
      }
      this[`${key}Status`] = true
    },

    // 服务商弹窗
    chooseProvider(type) {
      this.API.getUnbindServiceDisabledTypeById({ disabledTypeId: this.currentId }).then(res => {
        this.unbindProvider = res.data
        this.chooseProviderStatus = true
        if (type) {
          this.$nextTick(() => {
            this.bindProvider.map(b => {
              this.unbindProvider.map(u => {
                if (u.userId === b.userId) {
                  this.$refs.multipleTable.toggleRowSelection(u);
                }
              })
            })
          })
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },

    // 保存服务商
    saveProvider() {
      let serviceIds = this.bindProvider.map(p => p.userId)
      this.API.bindServiceDisabledType({ disabledTypeId: this.currentId, body: { serviceIds } }).then(res => {
        this.success('保存成功')
        this.providerStatus = false
      })
    },

    // 选择服务商
    handleChoose() {
      this.multipleSelection.map(({ userId, name }) => {
        if (this.bindProvider.findIndex(v => v.userId === userId) === -1) {
          this.bindProvider.push({ userId, name })
        }
      })
      this.chooseProviderStatus = false
    },

    // 删除服务商
    handleClose(e) {
      this.bindProvider.splice(this.bindProvider.findIndex(v => v.userId === e.userId), 1)
    },

    saveAppliance() {
      let categories = []
      this.chooseAppliance.map(item => {
        let { id: cid, keyword: categoryKey, label: categoryName } = this.eqList.find(v => v.keyword === item)
        categories.push({ cid, categoryKey, categoryName })
      })
      Object.assign(this.currentPara, { categories })
      this.API.bindDisabledTypeAssistCategoryById({ body: this.currentPara }).then(ret => {
        this.success('保存成功')
        this.applianceStatus = false
        this.chooseAppliance = []
      })
    }
  },
  mounted() {
    this.getPageData()
  }
}
</script>
<style scoped lang="scss">
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .el-tag {
    height: 40px;
    line-height: 38px;
    font-size: 14px;
    padding: 0 13px;
  }

  .el-button.is-circle {
    padding: 6px;
  }
</style>
